package yl.hs.bmipfull.service.finance.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import yl.hs.bmipfull.daos.finance.IBrickShipSettleMainDao;
import yl.hs.bmipfull.daos.finance.IReceivePaymentRecordDao;
import yl.hs.bmipfull.daos.salemanage.ISaleContractDao;
import yl.hs.bmipfull.pojo.finance.ContractInvoiceEntity;
import yl.hs.bmipfull.pojo.finance.MoneyTradeEntity;
import yl.hs.bmipfull.pojo.finance.ReceivePaymentRecordEntity;
import yl.hs.bmipfull.service.finance.IReceivePaymentRecordService;
import yl.hs.bmipfull.service.salemanage.impl.SaleContractServiceImpl;
import yl.hs.bmipfull.utils.DataState;
import yl.hs.bmipfull.utils.Tl;
import yl.hs.bmipfull.utils.datastate.ActionType;
import yl.hs.bmipfull.viewmodels.PageQuery;
import yl.hs.bmipfull.viewmodels.PageResult;
import yl.hs.bmipfull.viewmodels.SubmitModel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * @author 张凯
 * @apiNote 收款款信息-ServiceImpl
 * @since 2022/1/11 4:21 下午 创建
 */
@Service
public class ReceivePaymentRecordServiceImpl extends FinanceService implements IReceivePaymentRecordService {

    @Autowired
    IReceivePaymentRecordDao paymentRecordDao;
    @Autowired
    ISaleContractDao saleContractDao;
    @Autowired
    IBrickShipSettleMainDao mainDao;
    @Override
    public BigDecimal sumAmountByContractSn(String contractSn) {
        BigDecimal amount = paymentRecordDao.sumAmountByContractSn(contractSn);
        return amount != null ? amount:BigDecimal.ZERO;
    }

    @Override
    public boolean savePayment(SubmitModel submitModel) throws Exception {
        boolean result = false;
        ReceivePaymentRecordEntity entity;
        if (Tl.isEmpty(submitModel.getSn())) {
            //执行保存
            entity = new ReceivePaymentRecordEntity();
            var changelog = Tl.getChangeLog(entity, submitModel.getData());
            entity.setPYFieldValue();
            entity.setSn(queryNewSN(ReceivePaymentRecordEntity.class));
            entity.setOrganizationCode(getDepartSN());
            result = paymentRecordDao.insertNew(entity) > 0;
            if (result) {
                writeLog(ActionType.CREATE, entity.getSn(), changelog, Tl.toJson(entity), ReceivePaymentRecordEntity.class);
            }
        } else {
            //执行更新
            entity = paymentRecordDao.findBySN(submitModel.getSn());
            var changelog = Tl.getChangeLog(entity, submitModel.getData());
            entity.setPYFieldValue();
           result =  paymentRecordDao.update(entity) > 0;
            if (result) {
                writeLog(ActionType.UPDATE, entity.getSn(), changelog, Tl.toJson(entity), ReceivePaymentRecordEntity.class);
            }
        }
        return result;
    }


    @Override
    public boolean deleteBySn(String sn) {
        return paymentRecordDao.changeStateBySn(sn.split(","), DataState.DELETED.code) > 0;

    }

    @Override
    public List<ReceivePaymentRecordEntity> queryList(PageQuery pageQuery)throws Exception{
        pageQuery.checkSortSides(ReceivePaymentRecordEntity.class);
        return paymentRecordDao.queryPage(pageQuery.getQuery());
    }
    @Override
    public PageResult<Object> queryPage(PageQuery pageQuery,int rowNum,int page)throws Exception{
        pageQuery.checkSortSides(ReceivePaymentRecordEntity.class);
        var resultlist =  paymentRecordDao.queryPage(pageQuery.getQuery());
        pageQuery.getQuery().put("querySkip",(page-1)*rowNum);
        pageQuery.getQuery().put("pageSize",rowNum);
        PageResult<Object> result=new PageResult(rowNum);
        result.setRecords(paymentRecordDao.queryCountByCondition(pageQuery.getQuery()));
        result.setPage(page);
        List<Object> list = new ArrayList<>();
        var ss = paymentRecordDao.queryPage(pageQuery.getQuery());
        for (ReceivePaymentRecordEntity s : ss) {
            var map = s.toMap();
            var contract = saleContractDao.findBySN(s.getContractSN());
            if(contract!=null){
                map.put("CustomerName",contract.getCustomerName());
            }
            list.add(map);
        }
        result.setData(list);
        result.setObject(resultlist.stream().map(ReceivePaymentRecordEntity::getTradeAmount).reduce(BigDecimal::add));
        return result;
    }

    @Override
    public PageResult<ReceivePaymentRecordEntity> queryPageConstruction(PageQuery pageQuery, int rowNum, int page) throws Exception {
        pageQuery.checkSortSides(ReceivePaymentRecordEntity.class);
        pageQuery.getQuery().put("querySkip",(page-1)*rowNum);
        pageQuery.getQuery().put("pageSize",rowNum);
        PageResult<ReceivePaymentRecordEntity> result=new PageResult(rowNum);
        var snList=getConstructionContractSN();
        if(snList.size()<=0){
            result=new PageResult<>(rowNum);
            return  result;
        }
        result.setRecords(paymentRecordDao.queryCountByContractSN(pageQuery.getQuery(),snList));
        result.setPage(page);
        result.setData(paymentRecordDao.queryPageContractSN(pageQuery.getQuery(),snList));
        return result;
    }

    @Override
    public PageResult<MoneyTradeEntity> queryRecordPage(PageQuery pageQuery, int rowNum, int page){
        pageQuery.checkSortSides(ReceivePaymentRecordEntity.class);
        pageQuery.getQuery().put("querySkip",(page-1)*rowNum);
        pageQuery.getQuery().put("pageSize",rowNum);
        PageResult<MoneyTradeEntity> result=new PageResult(rowNum);
        result.setRecords(paymentRecordDao.queryRecordCountByQuery(pageQuery.getQuery()));
        result.setPage(page);
        result.setData(paymentRecordDao.queryRecordPageByQuery(pageQuery.getQuery()));
        return result;
    }

    @Override
    public List<ReceivePaymentRecordEntity> queryByContractSnAndTime(String contractSn) {
        var main = mainDao.LastBrickSettle(contractSn);
        return paymentRecordDao.queryByContractSnAndTime(contractSn,main.getSettleTime());
    }
}
